#!/usr/bin
# -*- coding: utf-8 -*-

from numpy import dot as npdot, \
                  cross as npcross
from numpy.linalg import norm as lanorm
import mod_parameters as params

TCI = params.PARAMS["testConstructorsInputs"]
EPSILON = params.PARAMS["geometricEpsilon"]

# ==============================================================================
class Edge():
    def __init__(self, startNdCId, endNdCId):
        self.sNd = startNdCId
        self.eNd = endNdCId
        self.nds = []

    def findIntermediateNodes(self, nodes):
        self.ins = []
        for I in nodes:
            SI = (I[0] - S[0], I[1] - S[1], I[2] - S[2])
            SI_v_u = vec(SI, self.u)
            if norm(SI_v_u) > EPSILON:
                pass
            else:
                EI = vector(E, I)
                a = sca(SI, EI)
                if a < 0:
                    self.ins.append((I, norm(SI) / self.l))

    def build(self, domNds, domNdsCIds):
        iSNd = domNdsCIds.index(self.sNd)
        iENd = domNdsCIds.index(self.eNd)
        S = domNds[iSNd].x
        E = domNds[iENd].x

        SE = E - S
        l = nplanorm(SE)
        try:
            uSE = SE / l
        except:
            raise ZeroDivisionError

        tmpEdgNdsCIds = {0.0:self.sNd,
                         1.0:self.eNd}
        for iNd, ndCId in enumerate(domNdsCIds):
            if ndCId != self.sNd and ndCId != self.eNd:
                N = domNds[iNd].x
                SN = N - S
                SEvSN = npcross(SE, SN)
                if nplanorm(SEvSN) < EPSILON:
                    # node is on the edge line, one has to test for owner
                    ln = npdot(SN, uSE)
                    if ln > -EPSILON and ln < l + EPSILON:
                        ln /= l
                        tmpEdgNdsCIds[ln] = ndCId
        #print tmpEdgNdsCIds
        for key in sorted(tmpEdgNdsCIds.iterkeys()):
            self.nds.append((tmpEdgNdsCIds[key], key))
        print self.nds
